package com.knowif.backend.controller;

import com.knowif.backend.service.UserOrderFlowService;
import com.knowif.backend.service.UserOrderHandleService;
import com.knowif.pojo.UserOrder;
import com.knowif.service.UserOrderSelectService;
import com.knowif.system.model.RestBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/wechat")
public class WechatOrderController {
    @Autowired
    private UserOrderFlowService userOrderFlowService;
    @Autowired
    private UserOrderHandleService userOrderHandleService;
    @Autowired
    private UserOrderSelectService userOrderSelectService;

    /**
     * 支付回调
     * */
    @PostMapping("/wechatPayResult")
    public void wechatPayResult(HttpServletRequest request, HttpServletResponse response){
        userOrderFlowService.wechatPayResult(request,response);
    }
    /**
     * 退款回调
     * */
    @PostMapping("/wechatRefundNotity")
    public synchronized void wechatRefundNotity(HttpServletRequest request, HttpServletResponse response){
        userOrderFlowService.payRefundNotify(response, request);
    }

    /**
     * 用户下单
     * */
    @PostMapping("/userConfirmOrder")
    public RestBean<?> userConfirmOrder(@RequestBody  UserOrder userOrder){
        return userOrderHandleService.userConfirmOrder(userOrder);
    }
    /***
     * 用户支付订单
     * */
    @PostMapping("/wechatPayOrder")
    public synchronized RestBean<?> wechatPayOrder(@RequestParam("openid") String openid,@RequestParam("body")  String body,@RequestParam("userOrderId") int userOrderId,HttpServletRequest request){
        return userOrderFlowService.wechatPayOrder(openid, body, request, userOrderId);
    }

    /**
     * 导师接单
     * */
    @PutMapping("/teacherAcceptOrder")
    public RestBean<?> teacherAcceptOrder(int userOrderId,String openid){
        return userOrderHandleService.teacherAcceptOrder(userOrderId,openid);
    }

    /**
     * 导师拒绝订单
     * */
    @PutMapping("/teacherCancelOrder")
    public RestBean<?> teacherCancelOrder(int userOrderId,String openid){
        return userOrderHandleService.teacherCancelOrder(userOrderId,openid);
    }

    /**
     * 导师确认 已交易
     * */
    @PutMapping("/teacherConfirmOrder")
    public RestBean<?> teacherConfirmOrder(int userOrderId){
        return userOrderHandleService.teacherConfirmOrder(userOrderId);
    }
    /**
     * 已交易 待评价
     * */
    @PutMapping("/waitEvalOrder")
    public RestBean<?> waitEvalOrder(int userOrderId){
        return userOrderHandleService.waitEvalOrder(userOrderId);
    }

    /***
     * 用户取消订单，退款
     * */
    @PutMapping("/userCancelOrder")
    public RestBean<?> userCancelOrder(int userOrderId){
        return userOrderHandleService.userCancelOrder(userOrderId);
    }

    /**
     * 查询用户订单
     * */
    @GetMapping("/selectOrderByUserId")
    public RestBean<?> selectOrderByUserId(int userId, int page, int amount){
        return userOrderSelectService.selectOrderByUserId(userId, page, amount);
    }
    /**
     * 订单流程展示
     * */
    @GetMapping("/selectUserOrderByOrderId")
    public RestBean<?> selectUserOrderByOrderId(int id){
        return userOrderSelectService.selectOrderStepById(id);
    }

    /**
     * 查询订单
     * */
    @GetMapping("/getUserOrderById")
    public RestBean<?> selectUserOrderById(int id){
        return userOrderSelectService.selectUserOrderById(id);
    }
    /**
     * 获取用户头像
     * */
    @GetMapping("/getTUserImageById")
    public RestBean<?> getTUserImageById(int userId){
        return userOrderSelectService.selectUserById(userId);
    }

    /**
     * 更新订单
     * */
    @PutMapping("updateUserOrder")
    public RestBean<?> updateUserOrder(@RequestBody UserOrder userOrder){
        return userOrderHandleService.updateUserOrderById(userOrder);
    }

    @GetMapping("getUserOrderTimeByIdStatus")
    public RestBean<?> getUserOrderTimeByIdStatus(int userOrderId,int status){
        return userOrderHandleService.getUserOrderStatusByOrderId(userOrderId,status);
    }

}
